/**
 * Copyright (C), 2020-2020, 软件公司
 * FileName: NewsServiceImpl
 * Author:   cakin
 * Date:     2020/5/6
 * Description: 新闻服务实现
 */
package com.cakin.newssystem.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cakin.newssystem.common.RequestException;
import com.cakin.newssystem.common.ResponseCode;
import com.cakin.newssystem.core.dto.NewsDTO;
import com.cakin.newssystem.core.entity.News;
import com.cakin.newssystem.core.mapper.NewsMapper;

import com.cakin.newssystem.core.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @ClassName: NewsServiceImpl
 * @Description: 新闻服务实现
 * @Date: 2020/5/6
 * @Author: cakin
 */
@Service
@Transactional
@Slf4j
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    /**
     * 新闻Mapper
     */
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public List<News> list( String username ) {
        return this.list(Wrappers.<News>lambdaQuery().eq(News::getUsername, username));
    }
// 下面是通过IService的saveOrUpdate实现统一新增或修改
//    @Override
//    public void add( NewsDTO newsDTO ) {
//        try {
//            News news;
//            try {
//                // 更新情况
//                news = detail(newsDTO.getUsername(), newsDTO.getTitle());
//                news.setContent(newsDTO.getContent());
//            } catch (Exception e) {
//                // 新增情况
//                news = new News();
//                news.setTitle(newsDTO.getTitle());
//                news.setContent(newsDTO.getContent());
//                news.setUsername(newsDTO.getUsername());
//            }
//            this.saveOrUpdate(news);
//        } catch (Exception e) {
//            log.info(e.getMessage());
//            throw new RequestException(ResponseCode.SQL_FAIL);
//        }
//    }
//    @Override
//    public News detail( String username, String title ) {
//        try {
//            return this.getOne(Wrappers.<News>lambdaQuery().eq(News::getUsername, username).eq(News::getTitle, title));
//        } catch (RuntimeException e) {
//            throw new RequestException(ResponseCode.TOO_MANY_RESULTS);
//        }
//    }

    // 先通过Lambda实现更新，如果更新失败，再是通过BaseMapper接口的insert实现新增，
    @Override
    public void add( NewsDTO newsDTO ) {
        try {
            // 更新情况：先更新一把，如果能更新成功，就说明是更新情况，否则就是新增情况
            boolean flag = new LambdaUpdateChainWrapper<News>(newsMapper)
                    .eq(News::getTitle, newsDTO.getTitle())
                    .eq(News::getUsername, newsDTO.getUsername())
                    .set(News::getContent, newsDTO.getContent()).update();
            log.info("flag is " + flag);
            // 新增情况：如果不能更新成功
            if (!flag) {
                News news = new News();
                news.setTitle(newsDTO.getTitle());
                news.setContent(newsDTO.getContent());
                news.setUsername(newsDTO.getUsername());
                newsMapper.insert(news);
            }
        } catch (Exception e) {
            throw new RequestException(ResponseCode.SQL_FAIL);
        }
    }

    @Override
    public News detail( String username, String title ) {
        try {
            LambdaQueryWrapper<News> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(News::getUsername, username)
                    .eq(News::getTitle, title);
            News news = newsMapper.selectOne(lambdaQueryWrapper);
            return news;
        } catch (Exception e) {
            throw new RequestException(ResponseCode.TOO_MANY_RESULTS);
        }
    }
}
